package cn.bouncyslime.graduationProject.nba.service;

import cn.bouncyslime.graduationProject.nba.dao.PlayerDataDao;
import cn.bouncyslime.graduationProject.nba.dao.PlayerInfoDao;
import cn.bouncyslime.graduationProject.nba.dto.*;
import cn.bouncyslime.graduationProject.nba.util.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张一卓
 * @date 2020/4/3 14:27
 **/
@Service
public class PlayerInfoService {

    private static final Logger logger = LoggerFactory.getLogger(PlayerInfoService.class);

    @Autowired
    PlayerDataDao playerDataDao;
    @Autowired
    PlayerInfoDao playerInfoDao;

    public List<PlayerInfoDto> listTopPlayers() {
        List<PlayerInfoDto> list = null;
        try {
            list = playerInfoDao.listTopPlayers();
        } catch (Exception e) {
            logger.warn("查询热门球员失败");
        }
        return list;
    }

    public ResponseData queryPlayer(String name, String role, String team) {
        List<PlayerInfoDto> list = null;
        if ((name == null || "".equals(name.trim())) &&
                (role == null || "".equals(role.trim())) &&
                (team == null || "".equals(team.trim()))) {
            return ResponseData.fail("查询条件不能为空");
        }
        try {
            list = playerInfoDao.queryPlayer(name, role, team);
            if (list == null || list.size() == 0) {
                return ResponseData.fail("没有查找到相关球员");
            }
        } catch (Exception e) {
            logger.warn("根据姓名查询球员失败 " + e.getMessage());
            return ResponseData.error("系统错误");
        }
        return ResponseData.success(list);
    }

    public ResponseData getPlayerById(Integer playerId) {
        PlayerInfoDto playerInfoDto = null;
        try {
            playerInfoDto = playerInfoDao.getPlayerById(playerId);
            if (playerInfoDto == null) {
                return ResponseData.fail("没有查找到对应球员");
            }
        } catch (Exception e) {
            logger.warn("根据 id 查询球员失败 " + e.getMessage());
            return ResponseData.error("系统错误");
        }
        return ResponseData.success(playerInfoDto);
    }

}
